﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpringEngine
{
    public class Endpoint
    {
        public ISpringable EndpointObject { get; set; }
        public Point Position { get; set; }
        public Velocity Velocity { get; set; } 
        public MotionVector Acceleration { get; set; }
        public double Mass { get; set; }

        #region Constructor. Prevents to create multiple Endpoint class containing same EndpointObject.
        static HashSet<Endpoint> endpoints = new HashSet<Endpoint>();

        public static Endpoint CreateEndpoint(ISpringable endpoint, double mass = 10)
        {
            if (endpoints.Any(e => e.EndpointObject == endpoint))
                return endpoints.First(e => e.EndpointObject == endpoint);

            Endpoint newEndpoint = new Endpoint(endpoint) {Mass = mass};
            endpoints.Add(newEndpoint);
            return newEndpoint;
        }

        private Endpoint(ISpringable endpoint)
        {
            Position = endpoint.GetPosition();
            EndpointObject = endpoint;
            Velocity = new Velocity();
        }
        #endregion

        public void SetPosition(double x, double y)
        {
            SetPosition(new Point(x,y));
        }

        public void SetPosition(Point position)
        {
            EndpointObject.SetPosition(position);
        }
    }
}
